/* Copyright (C) 2015, Heifai Information Technology Co., Ltd. All rights reserved. */
var resizeBanner = function() {
    var a = 1920,
        b = 512,
        c = $(window).width(),
        d = b * c / a,
        e = parseInt($(".d-banner .container").css("height")),
        f = .5 * (d - e - 40);
    $(".d-banner").css("height", d + "px"), $(".d-banner .container").css("margin-top", f)
};
$(window).load(function() {
    resizeBanner()
}), $(window).resize(function() {
    resizeBanner()
});
var reloadMasonry = function() {
    for (var a = $(".masonry-list"), b = 0; b < a.length; ++b) {
        var c = $(a[b]);
        c.imagesLoaded(function() {
            c.masonry({
                itemSelector: ".masonry-item"
            })
        })
    }
};
app.controller("publicRestaurantsBannerCtrl", ["$location", "$window", "$scope", "commonService", "infoRestaurantsService", function(a, b, c, d, e) {
    e.getInfo(function(a) {
        if (a && a.banners) {
            var b = (new Date).getHours(),
                c = a.banners.general;
            b >= 7 && 11 > b ? (c = c.concat(a.banners.morning), $(".d-banner").css("color", "#000")) : b >= 11 && 15 > b ? (c = c.concat(a.banners.noon), $(".d-banner").css("color", "#000")) : b >= 15 && 18 > b ? (c = c.concat(a.banners.nightfall), $(".d-banner").css("color", "#000")) : (c = c.concat(a.banners.night), $(".d-banner").css("color", "#EEE"));
            var e = d.randomOne(c);
            $(".d-banner").css("background-image", "url(" + e + ")")
        }
    })
}]), app.controller("publicRestaurantsCtrl", ["$location", "$scope", "$window", "loginService", "userCartService", "restaurantsService", "restaurantDishesService", "commonService", "infoService", function(a, b, c, d, e, f, g, h, i) {
    function j() {
        switch (k(o), o) {
            case "kitchen":
                $('#heifai-tab a[href="#heifai-kitchen"]').tab("show"), m();
                break;
            case "cookbox":
                $('#heifai-tab a[href="#heifai-cookbox"]').tab("show"), n();
                break;
            case "restaurants":
                $('#heifai-tab a[href="#restaurants"]').tab("show"), l();
                break;
            default:
                $("#heifai-tab a:first").tab("show"), m()
        }
    }

    function k(a) {
        "kitchen" == a ? (b.kitchenImg = "heifai-kitchen-active", b.cookboxImg = "heifai-cookbox", b.restaurantsImg = "restaurants", b.kitchenImgForApp = "heifai-kitchen-active-app", b.cookboxImgForApp = "heifai-cookbox-app", b.restaurantsImgForApp = "restaurants-app") : "cookbox" == a ? (b.kitchenImg = "heifai-kitchen", b.cookboxImg = "heifai-cookbox-active", b.restaurantsImg = "restaurants", b.kitchenImgForApp = "heifai-kitchen-app", b.cookboxImgForApp = "heifai-cookbox-active-app", b.restaurantsImgForApp = "restaurants-app") : "restaurants" == a ? (b.restaurantsImg = "restaurants-active", b.kitchenImg = "heifai-kitchen", b.cookboxImg = "heifai-cookbox", b.restaurantsImgForApp = "restaurants-active-app", b.kitchenImgForApp = "heifai-kitchen-app", b.cookboxImgForApp = "heifai-cookbox-app") : (b.kitchenImg = "heifai-kitchen-active", b.cookboxImg = "heifai-cookbox", b.restaurantsImg = "restaurants", b.kitchenImgForApp = "heifai-kitchen-active-app", b.cookboxImgForApp = "heifai-cookbox-app", b.restaurantsImgForApp = "restaurants-app")
    }

    function l() {
        Heifai.showLoading(), f.getRestaurants(function(a) {
            if (a && void 0 != a) {
                b.restaurants = [];
                var c = 0;
                i.getServerTime(function(d) {
                    c = d.timeNumber, i.getRestaurantCode(function(d) {
                        d && null != d && angular.forEach(a, function(a) {
                            if (a.active) {
                                var e = 0;
                                if (angular.forEach(a.tags, function(a) {
                                        (a == d.kitchen || a == d.cookbox) && e++
                                    }), 0 == e) {
                                    h.formatToHHmmss(a.servicePeriod.begin), h.formatToHHmmss(a.servicePeriod.end);
                                    a.offService = !(a.servicePeriod.begin <= c && c <= a.servicePeriod.end), b.restaurants.push(a)
                                }
                            }
                        }), Heifai.hideLoading()
                    })
                })
            } else Heifai.hideLoading()
        })
    }

    function m() {
        Heifai.showLoading(), f.getRestaurants(function(a) {
            if (a && void 0 != a) {
                b.kitchens = [];
                var c = 0;
                i.getServerTime(function(d) {
                    c = d.timeNumber, i.getRestaurantCode(function(d) {
                        d && null != d && angular.forEach(a, function(a) {
                            a.active && a.tags.length > 0 && angular.forEach(a.tags, function(e) {
                                if (e == d.kitchen) {
                                    h.formatToHHmmss(a.servicePeriod.begin), h.formatToHHmmss(a.servicePeriod.end);
                                    a.offService = !(a.servicePeriod.begin <= c && c <= a.servicePeriod.end), b.kitchens.push(a)
                                }
                            })
                        }), Heifai.hideLoading()
                    })
                })
            } else Heifai.hideLoading()
        })
    }

    function n() {
        Heifai.showLoading(), i.getRestaurantCode(function(a) {
            a && null != a && f.getRestaurantByCode(a.cookbox, function(a) {
                a && void 0 != a ? (b.restaurant = a, g.getRestaurantDishes(b.restaurant.id, function(a) {
                    a && void 0 != a ? (b.dishes = [], $(a).each(function(a, c) {
                        c.available && (c.addImg = "add", c.deleteImg = "delete", checkInCart(c.id, function(a) {
                            c.quantity = a
                        }), b.dishes.push(c))
                    }), Heifai.hideLoading()) : Heifai.hideLoading()
                })) : Heifai.hideLoading()
            })
        })
    }
    var o = a.url().replace("/", "");
    switch (o) {
        case "kitchen":
            break;
        case "cookbox":
            break;
        case "restaurants":
            break;
        default:
            c.location.href = "/"
    }
    d.getLogin(function(a) {
        b.loginInfo = a, j()
    }), $("#heifai-tab a").click(function(a) {
        a.preventDefault(), $(this).tab("show")
    }), b.changeTab = function(c) {
        k(c), "kitchen" == c ? (m(), a.path("/kitchen")) : "cookbox" == c ? (n(), a.path("/cookbox")) : "restaurants" == c && (l(), a.path("/restaurants")), b.$root.$broadcast("titleChange")
    }, b.mouseenter = function(a, b) {
        "add" == b ? a.addImg = "add-hover" : "delete" == b && (a.deleteImg = "delete-hover")
    }, b.mouseleave = function(a, b) {
        "add" == b ? a.addImg = "add" : "delete" == b && (a.deleteImg = "delete")
    }
}]);